package com.crm.controller.reportsController;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.crm.dao.ExecutivesDAO;
import com.crm.dao.SupplyOrderDAO;
import com.crm.entity.Business;
import com.crm.entity.Executives;

@Controller
@RequestMapping("/execBusiness")
public class ExecutiveBusController {
	protected final Log logger = LogFactory.getLog(getClass());
	ExecutivesDAO exdao=new ExecutivesDAO();
	SupplyOrderDAO sodao=new SupplyOrderDAO();
		@RequestMapping( "/show.do") 
		public ModelAndView show(HttpServletRequest req,HttpSession session, ModelAndView model) throws Exception{
			Business bus=(Business)session.getAttribute("business");
			if(bus==null)return new ModelAndView("/jsp/index.jsp");
			int busid=bus.getBusinessId();
			List<Executives> liex=exdao.findAll(busid);
			return model=new ModelAndView("/jsp/Executive Business.jsp")
			.addObject("submenu", "reports")
			.addObject("menuName","reports")
			.addObject("execList",liex);
		}
		@RequestMapping( "/getReport.do") 
		public ModelAndView getReport(HttpServletRequest request,HttpSession session, ModelAndView model) throws Exception{
			Business bus=(Business)session.getAttribute("business");
			if(bus==null)return new ModelAndView("/jsp/index.jsp");
			int busid=bus.getBusinessId();

			String exName=request.getParameter("execName");
			String from=request.getParameter("from");
			String to=request.getParameter("to");
			System.out.println("From Is:"+from+" To Is:"+to);
			String fromst = null;
			String tost = null;
			double totBus=0;
			if (from!=null && from!="" && to!=null && to!="") {
				String[] temp = from.split("-");
				String[] temp1 = to.split("-");
				fromst = temp[2] + temp[1] + temp[0];
				tost = temp1[2] + temp1[1] + temp1[0];
				 totBus=sodao.getExecBusinessByDate(busid,fromst,tost,exName);
			}else{
				 totBus=sodao.getExecBusiness(busid,exName);
			}
		  	
		  	Long tot=(long) totBus;
			
			return model=new ModelAndView("/jsp/ExecutiveBusinessRes.jsp")
			.addObject("submenu", "reports")
			.addObject("menuName","reports")
			.addObject("gTotal",tot);
		}


}
